﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Log;
using NetBay.Core.Extensions;
using Microsoft.Practices.EnterpriseLibrary.Data;
using NetBay.Core.DataAccess;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using Rio.Framework.RioManager;

#endregion

namespace Rio.Doc.DataAccess
{
    public class DA_Index
    {
        #region Enum
        /// <summary>
        /// List of DB functions to access data
        /// </summary>
        public enum DbFunction
        {
            [DescriptionAttribute("")]
            None,
            [DescriptionAttribute("P_INDEX_Get_ByFolderTypeId")]
            GetIndexByFolderType,
            [DescriptionAttribute("P_INDEX_Unicity_Get")]
            CheckIndexUnicity,
            [DescriptionAttribute("P_INDEX_Get_ByTableName")]
            GetIndex,
            [DescriptionAttribute("P_INDEX_Get_ByFolderId")]
            GetIndexValues
        }
        #endregion

        /// <summary>
        /// Check the list of index
        /// </summary>
        /// <param name="s_FolderTypeTableName">Name of the s_ folder type table.</param>
        /// <returns></returns>
        public static IDataReader GetIndex(string s_FolderTypeTableName)
        {
            var db = DbHelper.DataBase;
           
            var dbCommand = db.GetStoredProcCommand(DbFunction.GetIndex.szGetDescription());

            db.AddInParameter(dbCommand, "p_sFolderTypeTableName", DbType.String, s_FolderTypeTableName);          

            return db.ExecuteReader(dbCommand);
        }


        /// <summary>
        /// Check the unicity of an index
        /// </summary>
        /// <param name="p_lFolderTypeId">Folder Type identifier</param>
        /// <param name="p_lIndexId">The P_L index id.</param>
        /// <param name="p_sIndexValue">Value of the index</param>
        /// <param name="p_lFolderId">Folder Identifier</param>
        /// <returns></returns>
        public static IDataReader CheckUnicityOfIndexValue(long p_lFolderTypeId, long p_lIndexId, string p_sIndexValue, long p_lFolderId)
        {
            LogManager.Instance().LogDebug("(p_lFolderTypeId<" + p_lFolderTypeId + "> p_lIndexId<" + p_lIndexId + ">p_sIndexValue<" + p_sIndexValue + ">p_lFolderId<" + p_lFolderId + ">)...");

            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.CheckIndexUnicity.szGetDescription());
            db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int64, p_lFolderTypeId);
            db.AddInParameter(dbCommand, "p_lIndexId", DbType.Int64, p_lIndexId);
            db.AddInParameter(dbCommand, "p_sValue", DbType.String, p_sIndexValue);
            db.AddInParameter(dbCommand, "p_lFolderId", DbType.Int64, p_lFolderId);

            return db.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// Gets the type of the index by folder.
        /// </summary>
        /// <param name="p_lUserId">The P_L user id.</param>
        /// <param name="p_lFolderTypeId">The P_L folder type id.</param>
        /// <returns></returns>
        public static IDataReader GetIndexByFolderType(long p_lUserId, long p_lFolderTypeId)
        {
            var db = DbHelper.DataBase;
            
            var dbCommand = db.GetNewCommand(DbFunction.GetIndexByFolderType.szGetDescription());
            db.AddInParameter(dbCommand, "p_userId", DbType.Int64, p_lUserId);
            db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int64, p_lFolderTypeId);

            return db.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// Gets the type of the index by folder.
        /// </summary>
        /// <param name="p_lFolderTypeId">The P_L folder type id.</param>
        /// <returns></returns>
        public static IDataReader GetIndexByFolderType(long p_lFolderTypeId)
        {
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.GetIndexByFolderType.szGetDescription());
            db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int64, p_lFolderTypeId);

            return db.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// Gets the folder index values.
        /// </summary>
        /// <param name="p_lFolderTypeId">The P_L folder type id.</param>
        /// <param name="p_lFolderID">The P_L folder ID.</param>
        /// <returns></returns>
        public static IDataReader GetFolderIndexValues(long p_lFolderTypeId, long p_lFolderID)
        {
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.GetIndexValues.szGetDescription());
            db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int64, p_lFolderTypeId);
            db.AddInParameter(dbCommand, "p_lFolderId", DbType.Int64, p_lFolderID);

            return db.ExecuteReader(dbCommand);
        }
    }
}
